package com.gui.spring.aop.springProxy;

import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.beans.factory.annotation.Value;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class PointMatchDemo {

    public static void main(String[] args) throws NoSuchMethodException {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("execution(* foo(..))");

        boolean isMatchFoo = pointcut.matches(TargetService.class.getMethod("foo"), TargetService.class);
        boolean isMatchBar = pointcut.matches(TargetService.class.getMethod("foo"), TargetService.class);
        System.out.println(isMatchFoo);
        System.out.println(isMatchBar);

        System.out.println("===========================");
        MatcherHandler handler = new MatcherHandler();
        System.out.println(handler.matcher(TargetService.class.getDeclaredMethod("bar"), TargetService.class, Deprecated.class));
    }

    static class TargetService{

        @Deprecated
        @Value("111")
        public void foo(){
            System.out.println("foo()...");
        }


        public void bar(){
            System.out.println("bar()...");
        }
    }


    // 自定义的匹配器
    static class MatcherHandler{

        public boolean matcher(Method method, Class<?> classType, Class<? extends Annotation> targetAnnotation) {
            try {
                method.setAccessible(true);
                boolean methodResult = method.isAnnotationPresent(targetAnnotation);
                if (methodResult) {
                    return true;
                }
                return classType.isAnnotationPresent(targetAnnotation);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

    }
}
